package com.main.by.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.by.entity.vo.AdminLoginVO;
import com.main.by.entity.vo.CateVO;
import com.main.by.exception.ServiceException;
import com.main.by.service.CateService;
import com.main.by.util.MD5Util;
import com.main.by.util.StringUtil;

@Controller
@RequestMapping("/adminjsp/")
public class CateController {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private CateService cateService;
	
	@RequestMapping(value = "category", method = RequestMethod.GET)
	public String category(HttpServletRequest request){
		logger.info("查看所有分类信息-------");
		try {
			List<CateVO> cateInfo = cateService.findAllCate(request);
			request.getSession().setAttribute("cateInfoList", cateInfo);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "adminjsp/index";
		}
		return "adminjsp/category";
	}
	
	@RequestMapping(value = "saveCate", method = RequestMethod.POST)
	public void saveCate(HttpServletRequest request,HttpServletResponse response) throws IOException{
		logger.info("添加Cate信息-------");
		PrintWriter out = response.getWriter();
		String cateName = request.getParameter("addCategoty");
		try {
			 cateService.addCate(cateName);
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('添加任务分类失败');history.go(-1);</script>");
		}catch (Exception e) {
			out
			.print("<script type='text/javascript'>alert('添加任务分类失败');history.go(-1);</script>");
		}
		response.sendRedirect("category"); 
	}
	
	@RequestMapping(value = "/updateCategory", method = RequestMethod.GET)
	public String updateCategory(HttpServletRequest request) throws IOException{
		String errorMessage = "分类修改失败";
		String cateId =  request.getParameter("cateId");
		int id = 0;
		if (StringUtil.isNotBlank(cateId)) {
			id = Integer.parseInt(cateId);
		}
		logger.info("根据ID查找分类信息-------");
		CateVO cateInfo = new CateVO();
		try {
			cateInfo = cateService.findCateById(id);
			request.getSession().setAttribute("updateCateInfo", cateInfo);
		}catch (ServiceException serviceE){
			errorMessage = serviceE.getMessage();
			return "adminjsp/category";
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			return "adminjsp/category";
		}
		return "adminjsp/updateCategory";
	}
	
	@RequestMapping(value = "updateCateMethod", method = RequestMethod.POST)
	@ResponseBody 
	public void updateCateMethod(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String cateId =  request.getParameter("cateId");
		int id = 0;
		if (StringUtil.isNotBlank(cateId)) {
			id = Integer.parseInt(cateId);
		}
		String cateName =  request.getParameter("cateName");
		PrintWriter out = response.getWriter();
		CateVO cateInfo = new CateVO();
			cateInfo.setCateId(""+id);
			cateInfo.setCateName(cateName);
			
		logger.info("根据ID修改分类信息-------");
		try {
			cateService.updateCateMethod(cateInfo);
			response.sendRedirect("category");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('修改失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('修改失败');history.go(-1);</script>");
		}
		
	}
	
	@RequestMapping(value = "deleteCategory", method = RequestMethod.GET)
	public void deleteCategory(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String cateId =  request.getParameter("cateId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(cateId)) {
			id = Integer.parseInt(cateId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			cateService.deleteCateById(id);
			response.sendRedirect("category");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}
		
	}
}
